﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mads.CoreDomain;

namespace CassirApp
{
  public partial class Ticket2 : Form
  {
    public Ticket1 t1form;
    public FreeTicket[] tickets;
    public CarType[] carTypes;
    public Station stationDep;
    public Station stationArr;
    public string chosenTicket;

    public Ticket2(Ticket1 form, int id)
    {
      InitializeComponent();
      t1form = form;
      stationDep = t1form.massiveOfStations.FirstOrDefault(x => x.Name == t1form.startPoint);
      stationArr = t1form.massiveOfStations.FirstOrDefault(x => x.Name == t1form.finishPoint);
      tickets = t1form.client.GetTickets(t1form.sessionId, t1form.date.Date, stationDep, stationArr);
      carTypes = t1form.client.GetCarTypes(id);
      wagonType.Items.Clear();
      foreach (CarType type in carTypes)
      {
        wagonType.Items.Add(type.Name);
      }
      routeTextBox.Text = String.Empty;

      var ticketsGrouped =
        tickets.GroupBy(x => new
        {
          Route = x.RouteNumber,
          Car = x.CarNumber,
          Departure = x.DepartureTime
        }).Select(group => group.First());

      foreach (FreeTicket ticket in ticketsGrouped)
      {
        routeTextBox.Text += GenerateInfo(ticket) + "\n";
        routeTextBox.Text += "-----------------------\n";
      }
    }

    /// <summary>
    /// Генерим строку вывода данных
    /// </summary>
    /// <param name="ticket">билет</param>
    /// <returns></returns>
    private string GenerateInfo(FreeTicket ticket)
    {
      return String.Format(
        "Маршрут № {0}, тип вагона {1}, отправление {2} в {3}, прибытие {4} в {5}",
        ticket.RouteNumber, ticket.Type.Name,
        ticket.DepartureTime.ToShortDateString(),
        ticket.DepartureTime.ToShortTimeString(),
        ticket.ArrivalTime.ToShortDateString(),
        ticket.ArrivalTime.ToShortTimeString());
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
      string numberString = string.Empty;
      string routeString = raceBox.Text;
      string typeString = wagonType.Text;
      string placeString = sitType.Text;
      foreach (FreeTicket ticket in tickets)
      {
        if (placeString == "Верхнее")
        {
          if (ticket.Type.Name == typeString && ((ticket.SitNumber % 2) == 0) && ticket.RouteNumber == UInt32.Parse(routeString))
          {
            numberString += ticket.SitNumber.ToString() + " ";
          }
        }
        else
        {
          if (ticket.Type.Name == typeString && ((ticket.SitNumber % 2) == 1) && ticket.RouteNumber == UInt32.Parse(routeString))
          {
            numberString += ticket.SitNumber.ToString() + " ";
          }
        }
      }
      if (numberString == "")
      {
        numberString = "к сожалению, подходящих билетов не найдено";
      }

      chosenTicket = numberString;

      Ticket3 form = new Ticket3(this);
      form.Show();
    }
  }
}
